/*** DigiLogger Class
 *		Matthew Smith
 * 		http://digivation.net/programming/
 * 
 * 		Description: Allows you to log things with a nice little class!
 */

function digiLogger(fileName){
	// lets define our properties and set some defaults
	this.fileName = fileName; // holds the filename
	this.append = false; // true = append, false = overwrite
	this.enabled = true; // set this to false to disable the log
	this.timeStamp = false; // this enables or disables the time stamp
	this.headerEnabled = false; // this enables or disables the header
	//this.logLevel = 1; // *TODO* lets us do fun things!
	var initialized = false; // so we can see if the log has been setup
	this.logHeader = ""; // this can be set to contain some header text
	
	// define some methods...
	
	// log some text
	this.log = function (message) {
		if( this.enabled ) {
			if( !initialized ) { this.initialize(); } // if it's not setup, do it now
			var WriteMethod = 8;
			var fso = new ActiveXObject("Scripting.FileSystemObject"); // create FSO
			var output = fso.OpenTextFile(this.fileName, WriteMethod, false); // create output handle
			var reNewline = new RegExp("\n","g"); // find newlines
			
			// time stamp
			if( this.timeStamp ) {
				date = new Date();
				timeStamp = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "\t";
				output.Write(timeStamp);
				
				var outMessage = message.replace(reNewline, "\r\n" + timeStamp); // put the timestamp on newlines too
			} else { 
				var outMessage = message.replace(reNewline,"\r\n"); // replace them with writable newlines
			}
			
			output.Write(outMessage + "\r\n"); // output our message, with newline at the end
			
			output.Close(); // close the file
		}
	}
/*	
	this.purge = function () {
		// purge the log
	}
*/
	
	this.writeHeader = function () {
		if( (this.logHeader != "") && (this.headerEnabled == true) ) {
			if( !initialized ) { 
				this.initialize(); 
				return; // since the above routine writes the header, we don't need to do it again 
			}
			var WriteMethod = 8;
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			var output = fso.OpenTextFile(this.fileName, WriteMethod, false);
			var reNewline = new RegExp("\n","g"); // we want to convert our newlines
			var headerOutput = this.logHeader.replace(reNewline, "\r\n"); // do the conversion
			
			output.Write(headerOutput + "\r\n\r\n"); // output
			output.Close();
		}
	}
	
	// initialization
	this.initialize = function() {
		
		if( this.initialized ) { return; } // see if we've run this yet
		var WriteMethod;
	
		// set write method
		if( this.append == true ) {
			WriteMethod = 8; // append mode
		} else {
			WriteMethod = 2; // overwrite mode
		}
	
		var fso = new ActiveXObject("Scripting.FileSystemObject"); // create FSO
		var output = fso.OpenTextFile(this.fileName, WriteMethod, true); // create output handle
		
		if( (this.logHeader != "") && (this.headerEnabled == true) ) {
			var reNewline = new RegExp("\n","g"); // we want to convert our newlines
			var headerOutput = this.logHeader.replace(reNewline, "\r\n"); // do the conversion
			
			output.Write(headerOutput + "\r\n\r\n"); // output
		}
		
		output.Close(); // close the output
		this.initialized = true; // set our initialization flag!
	}
}